package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/pickservlet")
public class PickServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println(pickname());
        resp.getWriter().print(pickname());
    }
    public String pickname() throws IOException {
        InputStream resourcesStream = getClass().getResourceAsStream("/1.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(resourcesStream,"utf-8"));
        List<String> list =new ArrayList<>();
        String name = null;
        while ((name=reader.readLine())!=null){
            list.add(name);
        }
        reader.close();
        resourcesStream.close();
       return list.get((int)(Math.random()*list.size()));
    }
}
